<template>
  <div>
     <el-card class="box-card">
   <div class="header" slot="header"><span>{{isEdit?"编辑城市":"添加城市"}}</span></div>  
    <el-form ref="form" :model="form" label-width="80px">
            
       
      <el-form-item label="城市Id" v-if="isEdit">
        <el-input v-model="form.cityId" disabled></el-input>
      </el-form-item>
        <el-form-item label="城市名称">
        <el-input v-model="form.name" placeholder="请输入城市名称"></el-input>
      </el-form-item>
      
         <el-form-item label="城市拼音">
        <el-input v-model="form.pinyin" placeholder="请输入城市拼音"></el-input>
      </el-form-item>
  
      <el-form-item label="是否热门">
        <el-switch v-model="form.isHot" :active-text="form.isHot?'是':'否'"></el-switch>
      </el-form-item>   
    
    
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="onSubmit">{{isEdit?"提交修改":"提交添加"}}</el-button>
        <el-button @click=" removeText">{{isEdit?"取消":"重置"}}</el-button>
      </el-form-item>
    </el-form>
     </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {     
          name:"",
          pinyin:"",
        isHot:false,  
      },
       loading:false,  
    
    };
  },
  methods: {
    removeText(){
      if(this.isEdit)
      {
        this.$router.push('/city/list')
      }
      else{
        this.form.name="",
         this.form.pinyin=""
      }
    },


    //获取数据
    getdata(cityId){
  const url ='/city/findById?cityId='+cityId
    this.$axios.post(url).then(res=>{

      console.log("数据：",res)
      this.form=res.city
    }).catch(err=>{
      this.$message.error("修改失败");
    })
    },
    onSubmit() {
        this.loading = true;
        // let url = "/city/edit"
        let url = `/city/${this.isEdit?"edit":"add"}`
        console.log("this.form.name",typeof this.form.name)
        if((this.form.name !="")&&(this.form.pinyin !="")){
           this.$axios.post(url,this.form).then(res=>{
           
            console.log("res",res)
             this.$message.success(`${this.isEdit?"修改":"添加"}成功`);
             let $this=this
             setTimeout(function(){
                this.loading=false
               $this.$router.push('/city/list')
             },5000)
        }).catch(err=>{
            this.loading=false
            console.log("修改",err)
              this.$message.error(`${this.isEdit?"修改":"添加"}失败`);
        })  
        }
        else{
          this.loading=false
           this.$message.error("缺少城市名称或拼音");
        }     
    }
  },
  created() {
    //判断获取传过来的地址是否存在add,有则使用添加，否则是编辑
    
        console.log("this.$route",this.$route.path)
      
    var cityId=this.$route.params.cityId
   if(this.isEdit){
       this.getdata(cityId)
   }
  
  },
  computed: {
      isEdit(){
        console.log(this.$route)
          return   this.$route.path.includes('edit')
      }
  },
};
</script>> 

<style lang="less" scoped>
    .header{
    font-size: 22px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    }
    .el-input{
      width: 500px;
    }
      .box-card {
  width: 100%;
  height: 85vh;
}
</style>